package com.bear.cmcc.NetTestHTTP.util;

import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Xml;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 信息订阅相关工具类。
 * 
 * @author suchangjun
 * 
 */
public class InforSubUtil {

	/**
	 * 查询信息订阅接口。
	 * 
	 * @param context
	 * @return ArrayList<InfoSubBean> 一定不为null。
	 */
	public static ArrayList<InfoSubBean> getInfoSub(Context context) {
		ArrayList<InfoSubBean> returnStr = new ArrayList<InfoSubBean>();
		// TODO
		return returnStr;
	}

	// 解析频道列表
	/**
	 * 
	 * @param inStream
	 * @param isAll
	 *            是否查全部 true:在管理频道显示, false:在信息订阅显示
	 * @return
	 * @throws Throwable
	 */
	public static List<InfoSubBean> getChannels(InputStream inStream,
			boolean isAll) throws Throwable {
		List<InfoSubBean> channels = null;
		InfoSubBean channel = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inStream, "UTF-8");
		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				channels = new ArrayList<InfoSubBean>();
				break;

			case XmlPullParser.START_TAG:
				String name = parser.getName();
				if ("channel".equals(name)) {
					channel = new InfoSubBean();
				}
				if (channel != null) {
					if ("id".equals(name)) {
						channel.setId(parser.nextText());
					}
					if ("title".equals(name)) {
						channel.setTitle(parser.nextText());
					}
					if ("message".equals(name)) {
						channel.setMessage(parser.nextText());
					}
					if ("flag".equals(name)) {
						channel.setFlag(parser.nextText());
					}
					if ("path".equals(name)) {
						channel.setPath(parser.nextText());
					}
					if("number".equals(name)){
						channel.setNumber(parser.nextText());
					}
					System.out.println("path:"+channel.getPath());
				}

				break;

			case XmlPullParser.END_TAG:
				// if ("channel".equals(parser.getName())) {
				// channels.add(channel);
				// channel = null;
				// }
				if (isAll) {
					if ("channel".equals(parser.getName())) {
						channels.add(channel);
						channel = null;
					}
				} else {
					if ("channel".equals(parser.getName())
							&& "订阅".equals(channel.getFlag())) {
						channels.add(channel);
						channel = null;
					}
				}
				break;
			}
			eventType = parser.next();
		}

		inStream.close();
		return channels;
	}

	/**
	 * 保存订阅列表
	 * 
	 * @param channels
	 * @param outStream
	 * @throws Throwable
	 */
	public static void save(List<InfoSubBean> channels, OutputStream outStream)
			throws Throwable {
		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(outStream, "UTF-8");
		serializer.startDocument("UTF-8", true);
		serializer.startTag(null, "channels");
		for (InfoSubBean channel : channels) {
			//是否添加频道选项
			if(channel.getIsAddChannel()==1){
				continue;
			}
			
			serializer.startTag(null, "channel");

			serializer.startTag(null, "id");
			serializer.text(channel.getId());
			serializer.endTag(null, "id");

			serializer.startTag(null, "title");
			serializer.text(channel.getTitle());
			serializer.endTag(null, "title");

			serializer.startTag(null, "message");
			serializer.text(channel.getMessage());
			serializer.endTag(null, "message");

			serializer.startTag(null, "flag");
			serializer.text(channel.getFlag());
			serializer.endTag(null, "flag");

			serializer.startTag(null, "path");
			serializer.text(channel.getPath());
			serializer.endTag(null, "path");

			serializer.startTag(null, "number");
			serializer.text(channel.getNumber());
			serializer.endTag(null, "number");
			
			serializer.endTag(null, "channel");
		}
		serializer.endTag(null, "channels");
		serializer.endDocument();
	}
	
	/**
	 * 取asset目录下的图片
	 * @param fileName
	 * @param context
	 * @return
	 */
	public static Bitmap getImageFromAssetsFile(String fileName,Context context)  {  
        Bitmap image = null;  
        AssetManager am = context.getResources().getAssets();
        InputStream is = null;
        try  {  
            is = am.open(fileName);  
            image = BitmapFactory.decodeStream(is);  
        }catch (Exception e){  
            e.printStackTrace();  
        }finally{
        	if(is!=null)
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
        }  
    
        return image;  
    
    }  
	
}
